package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysUserDevelop;
import com.ruoyi.system.service.ISysUserDevelopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发展党员基本信息Controller
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/system/user/develop")
public class SysUserDevelopController extends BaseController
{
    @Autowired
    private ISysUserDevelopService sysUserDevelopService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询发展党员基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:develop:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysUserDevelop sysUserDevelop)
    {
        setStartPage(sysUserDevelop.getPageNum(),sysUserDevelop.getPageSize());
        List<SysUserDevelop> list = sysUserDevelopService.selectSysUserDevelopList(sysUserDevelop);
        return getDataTable(list);
    }

    /**
     * 获取发展党员基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:develop:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysUserDevelopService.selectSysUserDevelopById(id));
    }

    /**
     * 新增发展党员基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:develop:add')")
    @Log(title = "发展党员基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserDevelop sysUserDevelop)
    {
        if (StringUtils.isNotEmpty(sysUserDevelop.getPhonenumber()) && !sysUserDevelopService.checkPhoneUnique(sysUserDevelop))
        {
            return error("新增用户'" + sysUserDevelop.getNickName() + "'失败，手机号码已存在");
        }
        SysUser user = new SysUser();
        user.setPhonenumber(sysUserDevelop.getPhonenumber());
        if (StringUtils.isNotEmpty(sysUserDevelop.getPhonenumber()) && !sysUserService.checkPhoneUnique(user))
        {
            return error("新增用户'" + sysUserDevelop.getNickName() + "'失败，手机号码已存在");
        }
        sysUserDevelop.setCreateBy(String.valueOf(getUserId()));
        return toAjax(sysUserDevelopService.insertSysUserDevelop(sysUserDevelop));
    }

    /**
     * 修改发展党员基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:develop:edit')")
    @Log(title = "发展党员基本信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysUserDevelop sysUserDevelop)
    {
        if (StringUtils.isNotEmpty(sysUserDevelop.getPhonenumber()) && !sysUserDevelopService.checkPhoneUnique(sysUserDevelop))
        {
            return error("编辑用户'" + sysUserDevelop.getNickName() + "'失败，手机号码已存在");
        }
        SysUser user = new SysUser();
        user.setPhonenumber(sysUserDevelop.getPhonenumber());
        if (StringUtils.isNotEmpty(sysUserDevelop.getPhonenumber()) && !sysUserService.checkPhoneUnique(user))
        {
            return error("编辑用户'" + sysUserDevelop.getNickName() + "'失败，手机号码已存在");
        }
        return toAjax(sysUserDevelopService.updateSysUserDevelop(sysUserDevelop));
    }

    /**
     * 删除发展党员基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:develop:remove')")
    @Log(title = "发展党员基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(sysUserDevelopService.deleteSysUserDevelopById(id));
    }
}
